package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.Country;
import com.wontube.smr.dao.mapper.CountryMapper;
import com.wontube.smr.pub.DefaultDBHelper;

/**
 * @author yuechao
 *
 */
public final class CountryDao {
	private CountryDao() {		
	}
	
	public static boolean hasCountryCode(String countryCode){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			CountryMapper mapper = session.getMapper(CountryMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("country_code=", countryCode);
			return mapper.countByExample(ex) > 0;			
		} finally {
			session.close();
		}
	}
	
	public static List<Country> listCountries() {
		// 批量session
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			CountryMapper mapper = session.getMapper(CountryMapper.class);
			QueryExample ex = new QueryExample();
			ex.setOrderByClause("country.country_code asc");
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
	
	public static int addCountry(Country country){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			CountryMapper mapper = session.getMapper(CountryMapper.class);
			return mapper.insert(country);
		} finally {
			session.close();
		}
	}
	
	public static int deleteCountry(String id){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			CountryMapper mapper = session.getMapper(CountryMapper.class);
			return mapper.deleteByPrimaryKey(id);
		} finally {
			session.close();
		}
	}
}
